/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
 
db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.db4ofiletype;

import com.db4o.nb.api.Db4oProvider;
import java.io.IOException;
import java.util.logging.Logger;
import org.openide.cookies.CloseCookie;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.OpenCookie;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.text.DataEditorSupport;
import org.openide.windows.CloneableOpenSupport;

/*
 * Really simple implementation of OpenCookie, EditorCookie, 
 * CloseCookie and managing SaveCookie.
 *
 * @author klevgert
 */
public class Db4oEditorSupport extends DataEditorSupport 
    implements OpenCookie, EditorCookie, CloseCookie {

    private static Logger LOG = Logger.getLogger(Db4oEditorSupport.class.getName());
  
    public Db4oEditorSupport (Db4oDataObject obj) {
        super (obj, new Db4oEditorEnv (obj));
    }

    public static Db4oEditorSupport create(Db4oDataObject obj) {
        return new Db4oEditorSupport(obj);
    }
    
    protected boolean notifyModified () {
        boolean retValue = super.notifyModified();
        if (retValue) {
            Db4oDataObject obj = (Db4oDataObject)getDataObject();
            obj.ic.add(this.env);
        }
        return retValue;
    }

    protected void notifyUnmodified () {
        super.notifyUnmodified();
        Db4oDataObject obj = (Db4oDataObject)getDataObject();
        obj.ic.remove(env);
    }

    private static final class Db4oEditorEnv extends DataEditorSupport.Env implements SaveCookie {
        private static final long serialVersionUID = 6593415381104273008L;

        public Db4oEditorEnv (DataObject obj) {
            super (obj);
        }

        public void save () throws IOException {

          Db4oEditorSupport ed = (Db4oEditorSupport)this.findCloneableOpenSupport();
          ed.saveDocument();

          Db4oProvider db4o = (Db4oProvider)getDataObject().getCookie(Db4oProvider.class);
          if( db4o != null ) {
            db4o.save();
          }

          getDataObject ().setModified (false);
        }

        protected FileObject getFile () {
            return getDataObject ().getPrimaryFile ();
        }

        protected FileLock takeLock () throws IOException {
            return ((Db4oDataObject) getDataObject ()).getPrimaryEntry ().takeLock ();
        }

        public CloneableOpenSupport findCloneableOpenSupport () {
            // must be sync with cookies.add(EditorCookie.class, factory);
            // #12938 XML files do not persist in Source editor
            return (CloneableOpenSupport) getDataObject ().getCookie (EditorCookie.class);
        }
    }
}
